/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef BUSPROXY_SYSTEM_MEMORY_COLLECTOR_H
#define BUSPROXY_SYSTEM_MEMORY_COLLECTOR_H

#include <atomic>
#include "common/utils/proc_fs_tools.h"
#include "status/status.h"

namespace functionsystem {

class SystemMemoryCollector : public litebus::ActorBase {
public:
    explicit SystemMemoryCollector(const std::string &name);
    ~SystemMemoryCollector() override = default;
    uint64_t GetLimit() const;
    uint64_t GetCurrent() const;
    void SetLimit();
    void SetCurrent();
    void RefreshActualMemoryUsage();
    void StopRefreshActualMemoryUsage();
    // for test
    [[maybe_unused]] void SetProcFSTools(const std::shared_ptr<ProcFSTools> &procFSTools)
    {
        procFSTools_ = procFSTools;
    }

protected:
    void Init() override;
    void Finalize() override;

private:
    uint64_t GetMemoryUsage(const std::string &path);
    uint64_t GetRssUsage(const std::string &path) const;
    std::string rssPath_;
    std::shared_ptr<ProcFSTools> procFSTools_ { nullptr};
    std::atomic<uint64_t> limitUsage_ = 0;
    std::atomic<uint64_t> currentUsage_ = 0;
    litebus::Timer nextTimer_;
};
}  // namespace functionsystem
#endif  // BUSPROXY_SYSTEM_MEMORY_COLLECTOR_H